*******************************************************************;
***figure1.sas                                			***;
*******************************************************************;
***This program creates data for both panels of Figure 1 in the ***;
***Davis, Grim, Haltiwanger, and Streitwieser 2013 RESTAT paper.***;
*******************************************************************;
***Output:                                                      ***;
***   csv/figure1.csv                                           ***;
*******************************************************************;

options ls=80 compress=yes;

****START MACROS****;

%macro stat(var, wt);

proc univariate data=gs6300 round=0.001 noprint vardef=wdf;
        var &var;
        by year;
        weight &wt;
        output out=&var p10=p10 mean=mean p90=p90 std=std n=n nmiss=nmiss;

data &var;
        set &var;
        disp=p90-p10;

proc print data=&var;
        title1 "&var";

data &var._&wt;
        set &var;

proc datasets lib=work;
	delete &var;
run;
quit;

%mend stat;

****START PROGRAM****;

data gs6300;
        set elec.gs6300 (keep = lpe wtpe wttvs tvs ppn year); *This is a confidential establishment-level dataset - see README file for more information;

proc sort data=gs6300;
	by year;

%stat(lpe, wttvs)
%stat(lpe, wtpe)

data lpe_ww (keep = year disp_ww std_ww);
	set lpe_wttvs;
	disp_ww = disp;
	std_ww = std;

data lpe_wp (keep = year disp_wp std_wp);
	set lpe_wtpe;
	disp_wp = disp;
	std_wp = std;

data lpe;
	merge lpe_ww lpe_wp;
	by year;

proc print data=lpe;
	title1 "Figure 1 Numbers";

proc export data=lpe outfile="csv/figure1.csv" dbms=csv replace;
run;
quit;

run;
	 

